package hu.monitor.remoting;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Invoker {
	private final InvocationContext context;
	private final Object targetBean;

	public Invoker(final InvocationContext context, final Object targetBean) {
		if (context == null) {
			throw new IllegalArgumentException("context is null");
		}

		if (targetBean == null) {
			throw new IllegalArgumentException("targetBean is null");
		}

		this.context = context;
		this.targetBean = targetBean;
	}


	public Object invoke() throws Throwable {
		final Method method = this.context.getMethod();
		final Object[] args = this.context.getArgs();
		try {
			return method.invoke(this.targetBean, args);
		} catch (final InvocationTargetException ex) {
			throw ex.getCause();
		}
	}

}
